import { defineStore } from "pinia";
import { computed, ref } from "vue";
import { getCurrentUser } from "@/api/user";

export const userLoginUserStore = defineStore("counter", () => {
  const loginUser = ref<any>({
    userName: "defaultName",
    userRole: "user",
  });
  //ref返回一个响应式对象,该对象包含一个value属性，该属性是响应式的。响应式意思：
  //远程获取登录用户信息
  async function fetchLoginUser() {
    const res = await getCurrentUser();
    if (res.data.code == 0 && res.data.data) {
      loginUser.value = res.data.data;
    } /*else {
      setTimeout(() => {
        loginUser.value = {
          username: "小黑子",
          id: 1,
        };
      }, 3000);
    }*/
  }

  //单独设置信息
  function setLoginUser(user: any) {
    loginUser.value = user;
  }
  return { loginUser, fetchLoginUser, setLoginUser };
});
